package top.carrotguo.weibo.ui.person;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.adapter.PersonToolsAdapter;
import top.carrotguo.weibo.bean.PersonListBean;
import top.carrotguo.weibo.bean.UserInfo;
import top.carrotguo.weibo.contract.PersonContract;
import top.carrotguo.weibo.presenter.PersonPresenter;
import top.carrotguo.weibo.ui.base.BaseActivity;
import top.carrotguo.weibo.ui.base.MVPBaseFragment;

public class PersonFragment extends MVPBaseFragment<PersonContract.View,PersonPresenter>
    implements PersonContract.View{

    @BindView(R.id.rv_tools)
    RecyclerView rvTools;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.tv_blog_count)
    TextView tvBlogCount;
    @BindView(R.id.tv_stars)
    TextView tvStars;
    @BindView(R.id.tv_follows)
    TextView tvFollows;

    private ArrayList<PersonListBean> personList = new ArrayList<>();
    private PersonToolsAdapter mAdapter;
    private LinearLayoutManager mManager;
    private UserInfo user;

    //微信运动  更多

    @Override
    protected PersonPresenter createPresenter() {
        return new PersonPresenter();
    }

    @Override
    protected void getBundleExtras(Bundle bundle) {
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_person,container,false);
    }

    @Override
    protected void initLogic() {
        ButterKnife.bind(this,mRootView);
        initListView();
        //初始个人信息
        initPersonInfo();
    }

    /**
     * 初始化个人信息
     */
    private void initPersonInfo() {
        user = ((BaseActivity)mContext).user;
        if (user != null) {
            Glide.with(mContext)
                    .load(user.getAvatar_large())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(ivHead);
            tvName.setText(user.getName());
            tvDes.setText("简介："+ user.getDescription()==null ? "" : user.getDescription());
            tvBlogCount.setText(""+ user.getStatuses_count());
            tvFollows.setText(""+ user.getFollowers_count());
            tvStars.setText(""+ user.getFriends_count());
        } else {
            Logger.e("获取用户为Null");
        }
    }

    /**
     * 个人页面下边的每一栏初始化
     */
    private void initListView() {
        PersonListBean bean1 = new PersonListBean(R.drawable.push_icon_app_small_1,"新的朋友",null,true);
        PersonListBean bean2 = new PersonListBean(R.drawable.push_icon_app_small_2,"微博等级","Lv13",false);
        PersonListBean bean3 = new PersonListBean(R.drawable.push_icon_app_small_3,"编辑资料",null,false);
        PersonListBean bean4 = new PersonListBean(R.drawable.push_icon_app_small_4,"我的相册","(18)",true);
        PersonListBean bean5 = new PersonListBean(R.drawable.push_icon_app_small_5,"我的点评",null,false);
        PersonListBean bean6 = new PersonListBean(R.drawable.push_icon_app_small_2,"我的赞","(32)",false);
        PersonListBean bean7 = new PersonListBean(R.drawable.push_icon_app_small_1,"微信支付",null,true);
        PersonListBean bean8 = new PersonListBean(R.drawable.push_icon_app_small_3,"微信运动",null,false);
        PersonListBean bean9 = new PersonListBean(R.drawable.push_icon_app_small_5,"更多",null,false);
        personList.add(bean1);
        personList.add(bean2);
        personList.add(bean3);
        personList.add(bean4);
        personList.add(bean5);
        personList.add(bean6);
        personList.add(bean7);
        personList.add(bean8);
        personList.add(bean9);
        mAdapter = new PersonToolsAdapter(mContext,personList);
        mManager = new LinearLayoutManager(mContext);
        rvTools.setLayoutManager(mManager);
        rvTools.setAdapter(mAdapter);

    }

    @OnClick(R.id.rl_person)
    public void readyGoPerson(View view){
        //跳转个人详情页
        Bundle bundle = new Bundle();
        bundle.putSerializable(PersonDetailActivity.USER,user);
        ((BaseActivity)mContext).readyGo(PersonDetailActivity.class,bundle);
    }
}
